package com.tyrant.macaulottery.base;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.igexin.sdk.PushManager;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.orhanobut.logger.Logger;
import com.tyrant.macaulottery.bean.PushBean;
import com.tyrant.macaulottery.push.DemoIntentService;
import com.tyrant.macaulottery.push.DemoPushService;

import java.util.List;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by heyue on 2017/3/12.
 */

public class MyApp extends Application {
    private static final String TAG = "GetuiSdkDemo";

    private static DemoHandler handler;
    private static Realm realm;
    public static void initImageLoader(Context context) {
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .denyCacheImageMultipleSizesInMemory()
                .threadPriority(Thread.NORM_PRIORITY - 2)
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .build();
        ImageLoader.getInstance().init(config);
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Logger.init("FastApp--------->");
        initImageLoader(this);

        PushManager.getInstance().initialize(this.getApplicationContext(), DemoPushService.class);
        PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), DemoIntentService.class);
        Realm.init(getApplicationContext());
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder().build();
//        Realm.deleteRealm(realmConfiguration);
        realm = Realm.getInstance(realmConfiguration);
        if (handler == null) {
            handler = new DemoHandler();
        }
    }

    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }

    public static class DemoHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            final String json = (String) msg.obj;
            Logger.e(json);
            try {
                final PushBean pushBean = new Gson().fromJson(json, PushBean.class);
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
//                    realm.createObject(PushBean.class, json);
                        realm.createObjectFromJson(PushBean.class, json);

                    }
                });
            } catch (Exception e) {
                Logger.e(e.getMessage());
            }
            List<PushBean> allPushBean = realm.where(PushBean.class).findAll();
            for (PushBean pushBean : allPushBean) {
                Logger.e(pushBean.getContent());
            }
//            switch (msg.what) {
//                case 0:
//
//                    break;
//
//                case 1:
//
//                    break;
//            }
        }
    }
}
